package com.hm.hall.commons.util;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 作者 luhong E-mail:luhong@hoomic.com
 * @version 创建时间：2015年11月17日 下午5:01:28
 * 类说明
 */
public class RequestUtil {

    // \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
    // 字符串在编译时会被转码一次,所以是 "\\b"    
    // \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)    
    static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
            + "|windows (phone|ce)|blackberry"
            + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
            + "|laystation portable)|nokia|fennec|htc[-_]"
            + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
    static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
            + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";

    //移动设备正则匹配：手机端、平板  
    static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
    static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);

    /**
     * 检测是否是移动设备访问
     *
     * @param request
     * @return true:移动设备接入，false:pc端接入
     */
    public static boolean judgeIsMoblie(HttpServletRequest request) {
        String userAgent;
        try {
            userAgent = request.getHeader("User-Agent").toLowerCase();
        } catch (Exception e) {
            userAgent = "";
        }
        // 匹配    
        Matcher matcherPhone = phonePat.matcher(userAgent);
        Matcher matcherTable = tablePat.matcher(userAgent);
        if (matcherPhone.find() || matcherTable.find()) {
            return true;
        } else {
            return false;
        }
    }

    public static void setReqAttribute(HttpServletRequest req, long userId, String otherParam, Object resp) {
        req.setAttribute("resp", resp);
        req.setAttribute("handleUserId", userId);
        req.setAttribute("handleRequestURI", req.getRequestURI());
        req.setAttribute("otherParam", otherParam);
    }

    /**
     * 获得大版本号如2.1
     */
    public static double getAppVersion(String q_version) {
        double version = 0.0;
        if (null == q_version || "".equals(q_version)) {
            return version;
        } else {
            try {
                version = Double.valueOf(q_version.substring(0, 3));
                return version;
            } catch (Exception e) {
                return version;
            }
        }
    }

    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        if (version1.contains("beta")) {
            version1 = version1.replace("beta", "");
        }
        if (version2.contains("beta")) {
            version2 = version2.replace("beta", "");
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");
        int index = 0;
        //获取最小长度值
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;
        //循环判断每位的大小
        while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            //如果位数不一致，比较多余位数
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }


}
